#include "lrcpage.h"
#include "ui_lrcpage.h"

#include <QIcon>
#include <QFile>
#include <QDebug>

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);

    //隐藏窗口标题栏和边框
    setWindowFlag(Qt::FramelessWindowHint);

    connect(ui->hideBtn, &QPushButton::clicked, this, [=]()
    {
        emit setLrcAmination();
    });

    ui->hideBtn->setIcon(QIcon(":/images/pull_down.png"));
}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseMusicLrcFile(const QString &lrcFilePath)
{
    //1.打开文件
    QFile file(lrcFilePath);
    if(!file.open(QFile::ReadOnly))
    {
        perror("open operation failed");
        return false;
    }

    //清空歌词
    lrcWordLines.clear();

    //2.一次读取一行 lrc 歌词 readLine
    while(!file.atEnd())
    {
        QString lrcWordLine = file.readLine(1024);

        //解析 lrc 时间
        //[10:17.94]那些失眠的人啊 你现在还好吗
        int start = 0, end = 0;
        end = lrcWordLine.indexOf(']', start);
        QString lrcTime = lrcWordLine.mid(start, end - start + 1);
        QString lrcWord = lrcWordLine.mid(end + 1);

        qint64 lineTime = 0;

        //解析出分秒毫秒
        start = 1, end = lrcTime.indexOf(':', start);
        lineTime += lrcTime.mid(start, end - start).toInt() * 1000 * 60;

        start = end + 1, end = lrcTime.indexOf('.', start);
        lineTime += lrcTime.mid(start, end - start).toInt() * 1000;

        start = end + 1, end = lrcTime.indexOf(']', start);
        lineTime += lrcTime.mid(start, end - start).toLongLong();

        //3.将改行依次保存
        lrcWordLines << LrcWordLine(lineTime, lrcWord);
    }
    for(auto e : lrcWordLines)
    {
        qDebug() << e.lrcTime << e.lrcContent;
    }
    return true;
}

void LrcPage::showLrcWordLine(qint64 time)
{
    //1.获取当前歌曲所在的时间在 lrcWordLines 中的索引
    int index = getLrcWordLineIndex(time);
    if(index < 0)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲暂无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLrcWordLine(index - 3));
        ui->line2->setText(getLrcWordLine(index - 2));
        ui->line3->setText(getLrcWordLine(index - 1));
        ui->lineCenter->setText(getLrcWordLine(index));
        ui->line4->setText(getLrcWordLine(index + 1));
        ui->line5->setText(getLrcWordLine(index + 2));
        ui->line6->setText(getLrcWordLine(index + 3));
    }


    //2.更新中间、前三行、后三行歌词到界面
}

int LrcPage::getLrcWordLineIndex(qint64 time)
{
    if(lrcWordLines.empty())
        return -1;
    if(time <= lrcWordLines[0].lrcTime)
        return 0;
    for(int i = 1; i < lrcWordLines.size(); i++)
    {
        if(time < lrcWordLines[i].lrcTime && time >= lrcWordLines[i - 1].lrcTime)
            return i - 1;
    }
    return lrcWordLines.size() - 1;
}


QString LrcPage::getLrcWordLine(int index)
{
    if(index < 0 || index >= lrcWordLines.size())
        return "";
    else return lrcWordLines[index].lrcContent;
}

void LrcPage::setMusicName(const QString &musicName)
{
    ui->musicName->setText(musicName);
}

void LrcPage::setMusicSinger(const QString &musicSinger)
{
    ui->musicSinger->setText(musicSinger);
}
